package com.zirenx.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zirenx.sys.model.SysDept;
import com.zirenx.utils.tree.Tree;

public class SysDeptService {

	private SysDept dao = new SysDept().dao();
	
	private final String querySql = "select * from "+dao.getTableName()+" t where 1=1 ";
	
	
	/**
	 * 分页查询
	 * @param pageNumber
	 * @param pageSize
	 * @param kv
	 * @return
	 */
	public Page<SysDept> paginate(int pageNumber,int pageSize,Kv kv) {
		StringBuffer sb = new StringBuffer(querySql);
		SqlPara sqlPara = new SqlPara();
		
		sqlPara.setSql(sb.toString());
		return dao.paginate(pageNumber, pageSize, sqlPara);
	}
	
	public List<SysDept> list(Map<String, Object> map){
		StringBuffer sb = new StringBuffer(querySql);
		SqlPara sqlPara = new SqlPara();
		
		sqlPara.setSql(sb.toString());
		return dao.find(sqlPara);
	}
	
	public List<SysDept> list(String parentId){
		return dao.find(querySql + " and t.parent_id = " + parentId);
	}
	
	public SysDept findById(String id) {
		return dao.findById(id);
	}
	
	public void delete(String id) {
		dao.deleteById(id);
	}
	
	public Tree<SysDept> tree(){
		Tree<SysDept> root = new Tree<SysDept>();
		List<SysDept> list = list("0");
		if(list != null && list.size() == 1) {
			SysDept sysDept = list.get(0);
			root = treeChildren(sysDept);
		}else {
			root.setId("-1");
			root.setParentId("");
			root.setHasParent(false);
			root.setChildren(true);
			root.setChecked(true);
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			root.setState(state);
			root.setText("顶级节点");
			List<Tree<SysDept>> treeList = new ArrayList<Tree<SysDept>>();
			if(list != null && list.size() > 0) {
				Tree<SysDept> tree = null;
				for (SysDept sysDept : list) {
					tree = treeChildren(sysDept);
					treeList.add(tree);
				}
			}
			root.setChildren(treeList);
		}
		return root;
	}
	
	public Tree<SysDept> treeChildren(SysDept sysDept) {
		Tree<SysDept> root = new Tree<SysDept>();
		root.setId(""+sysDept.getId());
		root.setParentId(""+sysDept.getParentId());
		if("0".equals(sysDept.getParentId().toString())){
			root.setHasParent(false);
		}else {
			root.setHasParent(true);
		}
		
		root.setChecked(false);
		
		Map<String, Object> state = new HashMap<>(16);
		state.put("opened", false);
		root.setState(state);
		root.setText(sysDept.getName());
		List<Tree<SysDept>> treeList = new ArrayList<Tree<SysDept>>();
		
		List<SysDept> list = list(sysDept.getId()+"");
		if(list!=null && list.size()>0) {
			for (SysDept dept : list) {
				Tree<SysDept> tree = new Tree<SysDept>();
				tree = treeChildren(dept);
				treeList.add(tree);
			}
		}
		
		root.setChildren(treeList);
		return root;
	}
	
	
	public void batchRemove(String[] ids) {
		if(ids!=null) {
			for (String id : ids) {
				if(id!=null) {
					delete(id);
				}
			}
		}
	}
}
